//
//  Utility.m
//  collection
//
//  Created by 张靖 on 12/25/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "Utility.h"
#import "CollectionDataManager.h"
#import "TagInfo.h"

#define DEFAULT_DATE_TIME_ZONE_FORMAT   (@"yyyy-MM-dd HH:mm:ss Z")
#define DEFAULT_DATE_TIME_FORMAT        (@"yyyy-MM-dd HH:mm:ss")
#define DEFAULT_DATE_DATE_FORMAT        (@"yyyy-MM-dd")

@implementation Utility

+ (NSString*)transTimeToCurrentFormat:(NSDate *)time {
    
    return [self convertTimeToRelativeTime:time];
}

+ (CGSize)properSizeForDisplay:(NSArray *)imageArray {
    
    if (imageArray == nil || [imageArray count] == 0) {
        return CGSizeZero;
    }
    
    UIImage* firstImg = [imageArray objectAtIndex:0];
    return firstImg.size;
}

+ (NSString*)tagNameFromIndex:(int)index {
    
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    NSArray* tagList = sharedDataManager.hotTagList;
    
    for (TagInfo* tag in tagList) {
        if (tag.index == index) {
            return tag.name;
        }
    }
    
    return nil;
}

+ (NSString*)convertAbsoluteTimeToRelativeTime:(NSString *)absoluteTime
{
    // To do
    NSDate* checkDate = [self convertStrToDate:absoluteTime];
    return [self convertTimeToRelativeTime:checkDate];
}

+ (NSDate*)convertStrToDate:(NSString*)dateStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:DEFAULT_DATE_TIME_ZONE_FORMAT];
    NSDate *date = [formatter dateFromString:dateStr];
    
    if (date == nil) 
    {
        [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
        [formatter setDateFormat:DEFAULT_DATE_TIME_FORMAT];
        date = [formatter dateFromString:dateStr];
        
        if (date == nil)
        {
            [formatter setDateFormat:DEFAULT_DATE_DATE_FORMAT];
            date = [formatter dateFromString:dateStr];
        }
    }
    
    [formatter release];
    return date;
}

+ (NSString*)convertTimeToRelativeTime:(NSDate*)time
{
    NSInteger seconds =  0 - [time timeIntervalSinceNow];
    NSInteger minutes = seconds / 60;
    NSInteger hours = minutes / 60;
    NSInteger days = hours / 24;
    NSInteger mounths = days / 30;
    
    if (seconds >= 0) 
    {
        if (mounths >= 1) 
        {
            return [NSString stringWithFormat:@"%d月前", mounths];
        }
        else if (days >= 1)
        {
            return [NSString stringWithFormat:@"%d天前", days];
        }
        else if (hours >= 1)
        {
            return [NSString stringWithFormat:@"%d小时前", hours];
        }
        else if (minutes >= 1)
        {
            return [NSString stringWithFormat:@"%d分钟前", minutes];
        }
        else
        {
            return @"刚刚";
        }
    }
    else 
    {
        if (mounths <= -1) 
        {
            return [NSString stringWithFormat:@"%d月后", -mounths];
        }
        else if (days <= -1)
        {
            return [NSString stringWithFormat:@"%d天后", -days];
        }
        else if (hours<= -1)
        {
            return [NSString stringWithFormat:@"%d小时后", -hours];
        }
        else if (minutes <= -1)
        {
            return [NSString stringWithFormat:@"%d分钟后", -minutes];
        }
        else
        {
            return @"马上";
        }
    }
}


@end
